package cn.ecnu.auction.service;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import cn.ecnu.auction.dao.UserDAO;
import cn.ecnu.auction.engine.IService;
import cn.ecnu.auction.engine.Context;
import cn.ecnu.auction.po.User;

public class UserService implements IService {

	public void executeService(Context context) throws Exception {
		
		JSONObject requestJson = context.getJsonRequestObject();
		String operation = requestJson.getString("operation");
		int returnValue = 0;
		String jsonString = null;
		int returnCode = 0;
		
		if(operation.equals("add")){
			//新增用户
			String userName = requestJson.getString("userName");
			String userPassword = requestJson.getString("userPassWord");
			String mobile = requestJson.getString("mobile");
			String email = requestJson.getString("email");
			
			returnValue = new UserDAO().addUser(userName, userPassword, mobile, email);
		}
		else if(operation.equals("update")){
			//修改用户信息
			int userId = requestJson.getInt("userId");
			String userPassword = requestJson.getString("userPassWord");
			String mobile = requestJson.getString("mobile");
			String email = requestJson.getString("email");
			
			returnValue = new UserDAO().editUser(userId, userPassword, mobile, email);			
		}
		else if(operation.equals("query")){
			//查询用户
			String userName = requestJson.getString("userName");
			jsonString = new UserDAO().getUserByUserName(userName);
			if(jsonString!= null)
				returnValue=1;
			else
				returnValue=-1;
			
		}
		else if(operation.equals("validate")){
			//登录验证
			String userName = requestJson.getString("UserName");
			String userPassword = requestJson.getString("UserPassword");
			
			returnValue = new UserDAO().checkUserLogin(userName, userPassword);			
		}
		
		//返回值
		JSONObject o = new JSONObject();
		o.put("returnCode", returnCode);
		o.put("returnValue", returnValue);
		context.setJsonResponseObject(o);
	}

}
